package yxy.game.pm2.active.command.payday;

import yxy.cherry.data.bean.actrecharge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_累充天数;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActPayDayInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

/**
 * 累冲天数信息
 * @author Dell
 */
@MessageType(proto = Command.ActPayDayInfo, classtype = ActiveMessage.ActPayDayInfoRequest.class)
public class CMD_Active_累冲天数_Info extends Command<ActPayDayInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, ActPayDayInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActPayDayInfoResponse.Builder response = ActiveMessage.ActPayDayInfoResponse.newBuilder();
		
		ActiveModule_累充天数 am = ActiveModule_累充天数.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		response.setPaydays(am.getDays());
		response.addAllGetids(am.getGetids());
		
		Map<String, actrecharge> armap = ActDataCenter.getAllData(actrecharge.class, playercode);
		for (actrecharge data : armap.values()) {
			ActiveMessage.PayDay.Builder build = ActiveMessage.PayDay.newBuilder();
			build.setId(data.getId());
			build.setCondition(data.getCondition());
			build.setPaycondition(data.getRechargemin());
			
			Collection<Currency> curs = new Currency().create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
			int canget = am.getGetids().contains(data.getId()) ? DrawStatus.hadget.getStatus() :
				am.getDays() >= data.getCondition() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);
			
			response.addPds(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActPayDayInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
